// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SA1401FieldsMustBePrivateBulbItem.cs" company="StyleCop for ReSharper Development Team">
//   Copyright (c) StyleCop for ReSharper Development Team. All rights reserved.
// </copyright>
// <summary>
//   Defines the SA1401FieldsMustBePrivateBulbItem type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace StyleCop.For.ReSharper.BulbItems.Maintainability
{
    #region Using Directives

    using JetBrains.ProjectModel;
    using JetBrains.ReSharper.Psi;
    using JetBrains.ReSharper.Psi.CSharp.Impl;
    using JetBrains.ReSharper.Psi.CSharp.Tree;
    using JetBrains.ReSharper.Psi.Tree;
    using JetBrains.TextControl;

    using StyleCop.For.ReSharper.BulbItems.Framework;
    using StyleCop.For.ReSharper.Core;

    #endregion

    internal class SA1401FieldsMustBePrivateBulbItem : V5BulbItemImpl
    {
        public override void ExecuteTransactionInner(ISolution solution, ITextControl textControl)
        {
            var element = Utils.GetElementAtCaret(solution, textControl);

            var containingElement = (IElement)element.GetContainingElement<IFieldDeclarationNode>(true) ?? element.GetContainingElement<IMultipleDeclarationNode>(true);

            if (containingElement == null)
            {
                var treeNode = (ITreeNode)element;

                containingElement = treeNode.PrevSibling;
            }

            ModifiersUtil.SetAccessRights(containingElement, AccessRights.PRIVATE);
        }
    }
}